/*
 *  point.cpp
 *  EPPI-0.0
 *
 *  Created by Juan Carlos Garcia Orden on 15/11/07.
 *
 */

#include "point.h"


Point::Point()
:	name(""),
	r(),
//	isInComponent(false),
	dofT(),
	dofTInertia(),
	dofR(),
	dofRInertia()
{}

// ***

Point::Point(string pname, vector<double> vc)
:	name(pname),
	r(vc),
//	isInComponent(false),
	dofT(),
	dofTInertia(),
	dofR(),
	dofRInertia()
{}

// ***

void Point::setDof(int type, int pos)
{
		if(type==0)		/** type=0: Translational dof */
		{
			dofT.set(pos,1);
//			cout << "acabo de asignar dof de traslacion en point " << name  << endl;
		}
		else if(type==1) dofR.set(pos,1);	/** type=1: Rotational dof */
		else cout << "Error assigning dofs at point " << name << "\n";
}

// ***

void Point::setDofInertia(int type, int pos)
{
	if(type==0)		/** type=0: Translational dof */
	{
		dofTInertia.set(pos,1);
	}
	else if(type==1) dofRInertia.set(pos,1);	/** type=1: Rotational dof */
	else cout << "Error assigning dofs at point " << name << "\n";
}

// ***

int Point::setTransNumbers(int num)
{
	int i;
	for(i=0; i< dofT.count(); i++) globalTNum.push_back(num+i);	
	return( num+=dofT.count() );
}
// ***

int Point::setRotNumbers(int num)
{
	int i;
	for(i=0; i< dofR.count(); i++) globalRNum.push_back(num+i);
	
	/** Optional report */
//	reportGlobalNumbers();
	return( num+=dofR.count() );
}

// ***

int Point::setTRNumbers(int num)
{
	int i;
	for(i=0; i< dofT.count(); i++) 
	{
		globalTNum.push_back(num+i);
//			cout << name << " " << num + i << endl;
	}
	for(i=0; i< dofR.count(); i++) globalRNum.push_back(num+dofT.count()+i);		
	/** Optional report */
//	reportGlobalNumbers();
	
	return( num+=(dofT.count()+dofR.count()) );
}

// ***

int Point::setInertiaNumbers(int num, vector<int>& v)
{	
	int i;
	for(i=0; i< dofTInertia.count(); i++) 
	{
		globalINum.push_back(num+i);
	}
	for(i=0; i< dofRInertia.count(); i++) 
	{
		globalINum.push_back(num + dofTInertia.count() + i);
	}
	// Check which dofs do not have inertia

//		cout << "dofTI " << dofTInertia << endl;
//		cout << "dofRI " << dofRInertia << endl;
	
	if(globalTNum.size()!=0)
	{
		for(i=0; i< dofTInertia.size() ; i++) 
		{
			if ( dofT[i]==1 && dofTInertia[i]==0)	v.push_back( globalTNum[i] );
		}
	}
	
	if(globalRNum.size()!=0)
	{
		for(i=0; i< dofRInertia.size(); i++) 
		{
			if ( dofR[i]==1 && dofRInertia[i] == 0)	v.push_back( globalRNum[i] );
		}
	}
	return( num+=(dofTInertia.count()) + (dofRInertia.count()) );
}

// ***

void Point::reportGlobalNumbers()
{
	int i;
	if(dofT.count()!=0 || dofR.count()!=0)
	{
		cout << "			Point " << name << " has the global dof numbers \n";
		cout << "				T:	";
		for(i=0; i< dofT.count(); i++) cout << globalTNum[i] << "  ";
		cout << "				R:	";
		for(i=0; i< dofR.count(); i++) cout << globalRNum[i] << "  ";
		cout << "\n";
	}
}

// ***

vector<int> Point::getGlobalNumbers()
{
	int i;
	vector<int> vNumbers;
	
//	cout << "Voy a push en vNumbers en point " << name << endl;
//				cout << "dofT.count " << dofT.count() << endl;
	for(i=0; i< dofT.count(); i++) vNumbers.push_back( globalTNum[i] );
//		cout << "Ya he hecho push en vNumbers en point " << name << endl;
//			cout << "vNumbers.size " << vNumbers.size() << endl;
	for(i=0; i< dofR.count(); i++) vNumbers.push_back( globalRNum[i] );
	
	return (vNumbers);
}

// ***

vector<int> Point::getGlobalInertiaNumbers()
{
	int i;
	vector<int> vNumbers;
	for(i=0; i< globalINum.size(); i++) 
	{
		vNumbers.push_back( globalINum[i] );
//		cout << "point " << name << " with inertia number " << globalINum[i] << endl;
	}
																		
	return (vNumbers);
}

// ***

void Point::updateDofs(vector<double> & q, vector<double> & deltaq)
{
	int i;
	
//	cout << "****** globalTNum.size " << globalTNum.size() << "\n";
	
	// Additive update
	for (i=0; i<dofT.count(); ++i)
	{
		q[ globalTNum[i] ] -= deltaq[ globalTNum[i] ];
	}
	// Multiplicative update
	for (i=0; i<dofR.count(); ++i)
	{
	}	
}